package com.candy.candycommon.utils;

import java.util.HashMap;

/**
 * @author suYanLong
 * @date 2022年09月29日 13:55
 */
public class ResultBean extends HashMap<String, Object> {

    static String keyCode = "code";

    static String keyMsg = "msg";

    static String keyData = "data";

    public static ResultBean ok() {
        return ok(null);
    }

    public static ResultBean ok(Object data) {
        return create(200, "success", data);
    }

    public static ResultBean error(String msg) {
        return create(201, msg, null);
    }

    public static ResultBean create(Integer code, String msg, Object data) {
        ResultBean resultBean = new ResultBean();
        resultBean.put(keyCode, code);
        resultBean.put(keyMsg, msg);
        resultBean.put(keyData, data);
        return resultBean;
    }

    public Integer getCode() {
        return super.containsKey(keyCode) ? Integer.valueOf(super.get(keyCode).toString()) : 201;
    }

    public String getMsg() {
        return super.containsKey(keyMsg) ? String.valueOf(super.get(keyMsg)) : "";
    }

    public Object getData() {
        return super.containsKey(keyData) ? super.get(keyData) : null;
    }

    public enum ExceptionEnums {

        duplicateKeyException(50, "主键冲突异常"),
        ;

        private Integer code;

        private String msg;

        ExceptionEnums(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }

}
